﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections;
using System.IO;
using System.Drawing;
using System.Text;


public partial class useradmin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (Session["name"] == null)
        {
            Response.Redirect("~/error.aspx?");
        }
        else
        {
            if (!IsPostBack)
            {
                this.bind();
            }
        }
        if (Session["power"].ToString() != "超级管理员")
        {

            Response.Redirect("~/PowerError.aspx?");
        }
    }
    public SqlConnection GetConnection()
    {
        string myStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();
        SqlConnection myConn = new SqlConnection(myStr);
        return myConn;
    }
    protected void bind()
    {
        SqlConnection myConn = GetConnection();
        myConn.Open();
        string sqlStr = "select UID as 账号 , UPassword as 密码,UPower as 权限,UName as 姓名, USex as 性别,UTel as 电话,UAdress as 地址,UDep as 所属仓库 from [Users]";
        SqlDataAdapter myDa = new SqlDataAdapter(sqlStr, myConn);
        DataSet myDs = new DataSet();
        myDa.Fill(myDs);
        GridView1.DataSource = myDs;
        GridView1.DataKeyNames = new string[] { "账号" };
        GridView1.DataBind();
        myDa.Dispose();
        myDs.Dispose();
        myConn.Close();
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Button1.Visible = true;
        Button2.Visible = true;
        Button3.Visible = true;

        Label1.Visible = false;
        Label2.Visible = false;
        Label4.Visible = false;
        Label5.Visible = false;
        Label6.Visible = false;
        Label7.Visible = false;
        Label8.Visible = false;
        Label9.Visible = false;
        Label10.Visible = false;
        TextBox2.Visible = false;
        TextBox3.Visible = false;
        TextBox4.Visible = false;
        DropDownList1.Visible = false;
        RadioButtonList1.Visible = false;
        Button4.Visible = false;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/adduser.aspx?");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Label1.Visible = true;
        Label2.Visible = true;
        Label4.Visible = true;
        Label5.Visible = true;
        Label6.Visible = true;
        Label7.Visible = true;
        Label8.Visible = true;
        Label9.Visible = true;
        Label10.Visible = true;
        TextBox2.Visible = true;
        TextBox3.Visible = true;
        TextBox4.Visible = true;
        DropDownList1.Visible = true;
        RadioButtonList1.Visible = true;
        Button4.Visible = true;
        SqlConnection myConn = GetConnection();
        myConn.Open();
        string sqlStr = "select * from [Users] where UID='" + GridView1.SelectedValue.ToString() + "'";
        SqlCommand myCmd = new SqlCommand(sqlStr, myConn);
        myCmd.ExecuteNonQuery();
        SqlDataReader read = myCmd.ExecuteReader();
        read.Read();
        Label2.Text = read["UID"].ToString().Trim();
        Label6.Text = read["UName"].ToString().Trim();
        TextBox2.Text = read["UTel"].ToString().Trim();
        TextBox3.Text = read["UAdress"].ToString().Trim();
        TextBox4.Text = read["UDep"].ToString().Trim();
        RadioButtonList1.SelectedValue = read["USex"].ToString().Trim();
        DropDownList1.SelectedValue = read["UPower"].ToString().Trim();
        read.Close();
        myConn.Close();
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        SqlConnection myConn = GetConnection();
        myConn.Open();
        string sqlStr = "delete from [Users] where UID='" + GridView1.SelectedValue.ToString() + "'";
        SqlCommand myCmd = new SqlCommand(sqlStr, myConn);
        myCmd.ExecuteNonQuery();
        myConn.Close();
        //Response.Write("<script>alert('删除成功!')</script>");
        Response.Redirect("~/useradmin.aspx?");
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        SqlConnection myConn = GetConnection();
        myConn.Open();
        string sqlStr = "update [Users] set UPower='" + DropDownList1.SelectedValue + "',USex='" + RadioButtonList1.SelectedValue + "',UTel='" + TextBox2.Text.Trim() + "' ,UAdress='" + TextBox3.Text.Trim() + "',UDep='" + TextBox4.Text.Trim() + "'where UName='" + Label6.Text.Trim() + "'";
        SqlCommand myCmd = new SqlCommand(sqlStr, myConn);
        myCmd.ExecuteNonQuery();
        myConn.Close();
        //Response.Write("<script>alert('修改成功!')</script>");
        Response.Redirect("~/success.aspx?");
    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/daochu.aspx?");
        //int rowCount = GridView1.Rows.Count;

        //string checkIDlink = "";


        //for (int i = 0; i < rowCount; i++)
        //{
        //    CheckBox tempChk = (CheckBox)GridView1.Rows[i].FindControl("CheckItem");
        //    HiddenField HidID = (HiddenField)GridView1.Rows[i].FindControl("HidID");
        //    if (tempChk.Checked == true)
        //    {
        //        checkIDlink += HidID.Value + "|";
        //    }
        //}


        //if (String.IsNullOrEmpty(checkIDlink.Trim()))
        //{
        //    string ErroMsg = @"<mce:script language=""javascript""><!--  alert(""No Row is Selected!"")  // --></mce:script>";
        //    return;
        //}
        //Application["xuanzhe1"] = checkIDlink;
        ////checkIDlink = checkIDlink.Substring(0, checkIDlink.LastIndexOf("|")); 
    }


}